/**
 * @author Javen 
 * @copyright 2018-11-30 18:01:03 javendev@126.com 
 * @description QQ玩一玩中使用ES6
 * 步骤1 添加 https://google.github.io/traceur-compiler/bin/traceur.js 并设置为插件
 * 步骤2 如果要使用async 需要添加 https://raw.githubusercontent.com/caolan/async/master/dist/async.min.js 并设置为插件
 * 
 * ES6 Promise 用法讲解
 * https://www.cnblogs.com/whybxy/p/7645578.html
 */


let temps = ['测试ES6', 'Java', 'Node.js', 'Python', 'IJPay'];
cc.Class({
    extends: cc.Component,

    properties: {

    },

    // onLoad () {},

    start() {},
    toBack() {
        cc.director.loadScene("Scene");
    },

    testAsync() {
        cc.log("开始测试testAsync");
        async.each(temps, function (item) {
            cc.log("item:" + item + " " + new Date().getTime() + " TODO...");
            //耗时操作
            setTimeout(function () {
                cc.log('执行完成:' + item);
            }, 2000);

        }.bind(this));
        cc.log("结束测试testAsync");

        cc.log("开始测试testAsync2");
        async.eachLimit(temps, 2, function (item) {
            cc.log("item:" + item + " " + new Date().getTime());
        }.bind(this));
        cc.log("结束测试testAsync2");

    },

    testPromise() {
        cc.log("testPromise...");
        this.runAsync()
            .then(function (data) {
                cc.log("回调:" + data);
                cc.log(xxx); //此处的xxx未定义
            })
            .catch(function (reason) {
                cc.log(reason);
            });
        cc.log("testPromise end...");
    },

    testPromise2() {
        this.runAsync1()
            .then(function (data) {
                cc.log(data);
                return this.runAsync2();
            }.bind(this))
            .then(function (data) {
                cc.log(data);
                return this.runAsync3();
            }.bind(this))
            .then(function (data) {
                cc.log(data);
                return "直接放回数据";
            })
            .then(function (data) {
                cc.log(data);
            });
    },

    testPromiseAll() {
        Promise
            .all([this.runAsync1(), this.runAsync2(), this.runAsync3()])
            .then(function (results) {
                cc.log(results);
            });
    },
    testPromiseCatch() {
        this.getNumber()
            .then(function (data) {
                cc.log('resolved:' + data);
            })
            .catch(function (reason) {
                cc.log('rejected:' + reason);
            });
    },



    runAsync() {
        var p = new Promise(function (resolve, reject) {
            //做一些异步操作
            setTimeout(function () {
                cc.log('执行完成');
                resolve('resolve 随便什么数据');
            }, 2000);
        });
        return p;
    },

    runAsync1() {
        var p = new Promise(function (resolve, reject) {
            //做一些异步操作
            setTimeout(function () {
                console.log('异步任务1执行完成');
                resolve('随便什么数据1');
            }, 1000);
        });
        return p;
    },
    runAsync2() {
        var p = new Promise(function (resolve, reject) {
            //做一些异步操作
            setTimeout(function () {
                console.log('异步任务2执行完成');
                resolve('随便什么数据2');
            }, 2000);
        });
        return p;
    },
    runAsync3() {
        var p = new Promise(function (resolve, reject) {
            //做一些异步操作
            setTimeout(function () {
                console.log('异步任务3执行完成');
                resolve('随便什么数据3');
            }, 2000);
        });
        return p;
    },
    getNumber() {
        var p = new Promise(function (resolve, reject) {
            //做一些异步操作
            setTimeout(function () {
                var num = Math.ceil(Math.random() * 10); //生成1-10的随机数
                if (num <= 5) {
                    resolve(num);
                } else {
                    reject('数字太大了');
                }
            }, 2000);
        });
        return p;
    },

    // update (dt) {},
});